package com.controller.after;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.ajax.AjaxRes;
import com.entity.Attachment;
import com.service.AttachmentService;
@Controller
@RequestMapping("/Attachment/")//附件控制器
public class AttachmentController {

	//附件
	@Autowired
	private AttachmentService fjService;
	
	@RequestMapping("/Delel")
	public AjaxRes Delel(Attachment Att){
		AjaxRes res=new AjaxRes();
		fjService.deleteAnd(Att);
		res.setResMsg("删除成功");
		return res;
	}
	
	
	/**
	 * 下载附件需要传递路径
	 * @param AttachmentPath
	 * @param session
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getFile")
	public void getFile(String AttachmentPath,  HttpSession session, HttpServletResponse response)
            throws IOException {
		System.out.println("进入下载--------------------------");
		System.out.println("AttachmentPath:"+AttachmentPath);
        String basePath = session.getServletContext().getRealPath("/"); // 获取基本路径    
        if (null!=AttachmentPath && !AttachmentPath.equals("")) {
            /* 第一步:根据文件路径获取文件 */
            File file = new File(basePath + AttachmentPath);
            System.out.println("判断文件是否存在-----------------------");
            System.out.println("path:-------------"+basePath + AttachmentPath);
            if (file.exists()) { // 文件存在
                System.out.println("文件存在-----------------------");
                /* 第二步：根据已存在的文件，创建文件输入流 */
                InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
                /* 第三步：创建缓冲区，大小为流的最大字符数 */
                byte[] buffer = new byte[inputStream.available()]; // int available() 返回值为流中尚未读取的字节的数量
                /* 第四步：从文件输入流读字节流到缓冲区 */
                inputStream.read(buffer);
                /* 第五步： 关闭输入流 */
                inputStream.close();

                String fileName = file.getName();// 获取文件名
                response.reset();
                response.setContentType("application/force-download");
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
                response.addHeader("Content-Length", "" + file.length());

                /* 第六步：创建文件输出流 */
                OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
                //response.setContentType("application/octet-stream");
                /* 第七步：把缓冲区的内容写入文件输出流 */
                outputStream.write(buffer);
                /* 第八步：刷空输出流，并输出所有被缓存的字节 */
                outputStream.flush();
                /* 第九步：关闭输出流 */
                outputStream.close();
            } //end  if (file.exists())
        } else {
            return;
        }
    }
}
